package edu.gatech.bsbmule.util;

import java.util.Iterator;
import java.util.Set;

import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.google.common.collect.Table;

/**
 * Converts a Table to a XYDataSet for plotting statistics
 * 
 * @author BSB
 *
 */
public class XYDatasetAdapter {
	
	public static XYDataset getDatasetFromTable(Table<Integer, String, StatisticsRecord> table)
	{
		
		Set<String> names = table.columnKeySet();
		Iterator<String> nameIterator = names.iterator();
		
		Set<Integer> rounds = table.rowKeySet();
		
		XYSeriesCollection dataset = new XYSeriesCollection();
		
		for (int i = 0 ; i < names.size() ; ++i) {
			
			String currentName = nameIterator.next();
			XYSeries series = new XYSeries(currentName);
			
			for (int j = 0 ; j < rounds.size() ; ++j) {
				series.add(j + 1, table.get(j + 1, currentName).getScore());
			}
			
			dataset.addSeries(series);
		}
		
		return dataset;
	}
}
